package app.auto;

import android.app.Application;
import android.os.Build;

import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.picasso.OkHttpDownloader;
import com.squareup.picasso.Picasso;

import org.xutils.x;

import java.net.InetAddress;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import app.auto.runner.base.DipUtil;
import app.auto.runner.base.SPrefUtil;
import app.auto.runner.base.framework.CrashHandler;
import app.auto.runner.base.utility.StorageUtil;
import imageloader.AuthImageDownloader;

public class Deploy extends AndroidInstance {


    public final static String CRASH_HANDLER = "crashhandler";
    public final static String PICASO = "picaso";
    public final static String XUTIL = "XUTIL";
    public final static String SPREF = "SPREF";
    public final static String DIPUTIL = "DIPUTIL";
    public final static String STORAGE_SD_DIR = "STORAGE_SD_DIR";
    public static final String IMAGE_LOADER = "IMAGE_LOADER";
    public static final String BMOB = "BMOB";
    public static final String BMOB_ID_FORAPP = "BMOB_ID_FORAPP";
    public static final String OKHTTP_SSL = "OKHTTP_SSL";
    private List list = new ArrayList();


//    public final static String SPREF = "SPREF";
//    public final static String SPREF = "SPREF";


    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }

    Map params;

    public Deploy addParams(Map map) {
        this.params = map;
        return this;
    }

    public Deploy addlist(List list) {
        this.list = list;
        return this;
    }

    public void init() {
        initPrivate();

    }

    private void initPrivate() {
        if (list == null) {
            return;
        }
        if (list.contains(CRASH_HANDLER)) {
            CrashHandler.getInstance().init(getContext());
        }
        if (list.contains(PICASO)) {
            Picasso.setSingletonInstance(new Picasso.Builder(getContext()).
                    downloader(new OkHttpDownloader(getUnsafeOkHttpClient()))
                    .build());
        }
        if (list.contains(XUTIL)) {
            x.Ext.init((Application) getContext());
        }
        if (list.contains(SPREF)) {
            SPrefUtil.iniContext(getContext());
        }
        if (list.contains(DIPUTIL)) {
            DipUtil.initCtx(getContext());

        }
        if (list.contains(STORAGE_SD_DIR)) {
            StorageUtil.initPackageDir(getContext());
        }
//        if (list.contains(OKHTTP_SSL)) {
//            getUnsafeOkHttpClient();
//        }

        if (list.contains(IMAGE_LOADER)) {
            ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(
                    getContext());
            config.imageDownloader(new AuthImageDownloader(getContext()));
        }
    }


    public OkHttpClient getUnsafeOkHttpClient() {

        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] chain,
                        String authType) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] chain,
                        String authType) throws CertificateException {
                }

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            }};

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts,
                    new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext
                    .getSocketFactory();

            OkHttpClient okHttpClient = new OkHttpClient();
            okHttpClient.setSslSocketFactory(sslSocketFactory);
            okHttpClient.setHostnameVerifier(new HostnameVerifier() {

                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;

                }
            });

            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
